﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#include "CellStableHeaders.h"
#include "CellSingleFileResultSet.h"
#include "CellCharBuffer.h"
#include "CellSingleFileStorage.h"

namespace cell
{
//---------------------------------------------------------------------------
SingleFileResultSet::SingleFileResultSet(void)
	: mCursor(-1)
	, mStorage(NULL)
	, mFileSize(0)
	, mBufOffset(0)
	, mBufLength(0)
	, mBufData(NULL)
{
}
//---------------------------------------------------------------------------
SingleFileResultSet::~SingleFileResultSet(void)
{
	if (NULL != mBufData)
	{
		CELL_DELETE mBufData;
		mBufData = NULL;
	}
}
//---------------------------------------------------------------------------
bool SingleFileResultSet::absolute(int cursor)
{
	if (cursor != 0)
		return false;

	mCursor = cursor;
	return true;
}
//---------------------------------------------------------------------------
bool SingleFileResultSet::relative(int cursor)
{
	return false;
}
//---------------------------------------------------------------------------
bool SingleFileResultSet::first(void)
{
	mCursor = 0;
	return true;
}
//---------------------------------------------------------------------------
bool SingleFileResultSet::last(void)
{
	mCursor = 0;
	return true;
}
//---------------------------------------------------------------------------
bool SingleFileResultSet::next(void)
{
	if (mCursor + 1 == 0)
	{
		mCursor += 1;
		return true;
	}

	return false;
}
//---------------------------------------------------------------------------
bool SingleFileResultSet::previous(void)
{
	return false;
}
//---------------------------------------------------------------------------
bool SingleFileResultSet::isFirst(void)
{
	return (mCursor == 0);
}
//---------------------------------------------------------------------------
bool SingleFileResultSet::isLast(void)
{
	return (mCursor == 0);
}
//---------------------------------------------------------------------------
char SingleFileResultSet::getChar(int index)
{
	return 0;
}
//---------------------------------------------------------------------------
char SingleFileResultSet::getChar(const String& label)
{
	return 0;
}
//---------------------------------------------------------------------------
int SingleFileResultSet::getInt(int index)
{
	return 0;
}
//---------------------------------------------------------------------------
int SingleFileResultSet::getInt(const String& label)
{
	return getInt64(label);
}
//---------------------------------------------------------------------------
long SingleFileResultSet::getLong(int index)
{
	return 0;
}
//---------------------------------------------------------------------------
long SingleFileResultSet::getLong(const String& label)
{
	return getInt64(label);
}
//---------------------------------------------------------------------------
int64 SingleFileResultSet::getInt64(int index)
{
	return 0;
}
//---------------------------------------------------------------------------
int64 SingleFileResultSet::getInt64(const String& label)
{
	if (mCursor != 0)
	{
		return -1;
	}

	if (label == SingleFileStorage::FIELD_SIZE)
	{
		return mFileSize;
	}
	else
	{
		return 0;
	}
}
//---------------------------------------------------------------------------
String SingleFileResultSet::getString(int index)
{
	return "";
}
//---------------------------------------------------------------------------
String SingleFileResultSet::getString(const String& label)
{
	return "";
}
//---------------------------------------------------------------------------
bool SingleFileResultSet::getBool(int index)
{
	return false;
}
//---------------------------------------------------------------------------
bool SingleFileResultSet::getBool(const String& label)
{
	if (mCursor != 0)
	{
		return false;
	}

	if (label == SingleFileStorage::FIELD_EXIST)
	{
		return (NULL != mBufData);
	}
	else
	{
		return false;
	}
}
//---------------------------------------------------------------------------
int SingleFileResultSet::getRaw(const String& label, char* dst, int offset, int length)
{
	return getRaw(label, dst, (int64)offset, (int64)length);
}
//---------------------------------------------------------------------------
int64 SingleFileResultSet::getRaw(const String& label, char* dst, int64 offset, int64 length)
{
	if (mCursor != 0
		|| label != SingleFileStorage::FIELD_DATA
		|| NULL == mBufData)
	{
		return 0;
	}
	else if (offset < 0 || length <= 0 || offset >= mFileSize)
	{
		return 0;
	}

	if (mFileSize > (int64)mStorage->mFetchSize)
	{
		bool invalid = false;

		// 处理大于 Fetch Size 大小的文件
		int64 bufEnd = mBufOffset + mBufLength;
		if (offset > bufEnd || offset < mBufOffset)
		{
			// 位置偏移偏离当前 Buffer 偏移位置
			invalid = true;
		}
		else if (offset + length > bufEnd)
		{
			invalid = true;
		}

		if (invalid)
		{
			int64 remaining = mFileSize - offset;
			mBufLength = (((int64)mStorage->mFetchSize) < remaining) ? mStorage->mFetchSize : remaining;
			mBufOffset = offset;

			if (NULL != mBufData)
				CELL_DELETE mBufData;
			mBufData = CELL_NEW CharBuffer(mBufLength);

			char* read = CELL_ALLOC_T(char, mBufLength, MEMCATEGORY_GENERAL);
			memset(read, 0x0, mBufLength);

			/* 以下代码仅测试用
			std::ifstream file(mFileName, std::ios::binary);
			file.seekg(mBufOffset);
			file.read(read, mBufLength);
			int rlen = file.gcount();
			file.close();
			*/

			FILE* fp = fopen(mFileName.c_str(), "rb");
			fseek(fp, mBufOffset, SEEK_SET);
			int rlen = fread(read, 1, mBufLength, fp);
			fclose(fp);

			// 文件数据写入缓存
			mBufData->put(read, rlen);
			CELL_FREE(read, MEMCATEGORY_GENERAL);

			mBufData->flip();
		}
	}

	int len = mBufData->get(dst, offset - mBufOffset, length);
	return len;
}
//---------------------------------------------------------------------------
void SingleFileResultSet::updateChar(int index, char value)
{
	// Nothing
}
//---------------------------------------------------------------------------
void SingleFileResultSet::updateChar(const String& label, char value)
{
	// Nothing
}
//---------------------------------------------------------------------------
void SingleFileResultSet::updateInt(int index, int value)
{
	// Nothing
}
//---------------------------------------------------------------------------
void SingleFileResultSet::updateInt(const String& label, int value)
{
	updateInt64(label, value);
}
//---------------------------------------------------------------------------
void SingleFileResultSet::updateLong(int index, long value)
{
	// Nothing
}
//---------------------------------------------------------------------------
void SingleFileResultSet::updateLong(const String& label, long value)
{
	updateInt64(label, value);
}
//---------------------------------------------------------------------------
void SingleFileResultSet::updateInt64(int index, int64 value)
{
	// Nothing
}
//---------------------------------------------------------------------------
void SingleFileResultSet::updateInt64(const String& label, int64 value)
{
	if (mCursor != 0
		|| mStatement != "write")
	{
		return;
	}

	if (label == SingleFileStorage::FIELD_SIZE)
	{
		if (mFileSize != value)
		{
			mFileSize = value;
			mStorage->changeSize(value);
		}
	}
}
//---------------------------------------------------------------------------
void SingleFileResultSet::updateString(int index, const String& value)
{
	// Nothing
}
//---------------------------------------------------------------------------
void SingleFileResultSet::updateString(const String& label, const String& value)
{
	// Nothing
}
//---------------------------------------------------------------------------
void SingleFileResultSet::updateBool(int index, bool value)
{
	// Nothing
}
//---------------------------------------------------------------------------
void SingleFileResultSet::updateBool(const String& label, bool value)
{
	// Nothing
}
//---------------------------------------------------------------------------
void SingleFileResultSet::updateRaw(const String& label, char* src, int offset, int length)
{
	updateRaw(label, src, (int64)offset, (int64)length);
}
//---------------------------------------------------------------------------
void SingleFileResultSet::updateRaw(const String& label, char* src, int64 offset, int64 length)
{
	if (mCursor != 0
		|| mStatement != "write"
		|| label != SingleFileStorage::FIELD_DATA)
	{
		return;
	}

	mStorage->write(src, offset, length);
}
//---------------------------------------------------------------------------
void SingleFileResultSet::fill(SingleFileStorage* storage, const String& statement,
	const String& fileName, int64 fileSize)
{
	mStorage = storage;
	mStatement = statement;
	mFileName = fileName;
	mFileSize = fileSize;
}
//---------------------------------------------------------------------------
void SingleFileResultSet::copyFileData(CharBuffer* src, size_t srcOffset, size_t srcLength)
{
	if (NULL == mBufData)
	{
		mBufData = CELL_NEW CharBuffer(src->capacity());
	}

	*mBufData = *src;
	mBufData->rewind();

	mBufOffset = srcOffset;
	mBufLength = srcLength;
}
} // end namespace cell
